﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FaithCMS;

namespace FaithCMSControls
{
    [Serializable]
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:TargetedLogin runat=server></{0}:TargetedLogin>")]
    public class TargetedLogin : CompositeControl, INamingContainer
    {
        //Internal controls
        protected TextBox textBoxUserName;
        protected TextBox textBoxPassword;
        protected Label lblUserName;
        protected Label lblPassword;
        protected DropDownList destList;
        protected Button submitButton;

        //Event declaration for User Authentication
        public event EventHandler UserAuthenticated;

        //Destination Dictionary
        Dictionary<string, string> dictionaryDestinations;

        //List of target locations
        List<string> targetLocations;

        //Text Property
        public string Text
        {
            get
            {
                return "Not Implemented";
            }

            set
            {
                try
                {
                    throw new NotImplementedException("This text property isn't being used. Child Control properties are being used instead to prevent ViewState clutter.");
                }
                catch (NotImplementedException nException)
                {
                    Console.WriteLine(nException.Message);
                }
            }
        }

        //Username Text Property
        public string UserText
        {
            get
            {
                return textBoxUserName.Text;
            }

            set
            {
                textBoxUserName.Text = value;
            }
        }

        //Password Text Property
        public string PasswordText
        {
            get
            {
                return textBoxPassword.Text;
            }

            set
            {
                textBoxPassword.Text = value;
            }
        }
        
        public string Destination
        {
            get
            {
                //Return the destList dropdown list selected value
                return destList.SelectedValue;
            }

            set
            {
                //Set the destList value
                foreach (ListItem i in destList.Items)
                {
                    //If the item exists in the list
                    if (destList.Items.Contains(i))
                    {
                        //Set the destList SelectedValue to the item after converting it to a string value
                        destList.SelectedValue = i.ToString();
                    }
                    else
                    {
                        //Otherwise, output a message that the value doesn't exist
                        Console.WriteLine("This value is not in the list.");
                    }
                }
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            dictionaryDestinations = new Dictionary<string, string>();

            targetLocations = new List<string>();

            //Add the locations (must be in the same order as the destList values)
            targetLocations.Add(@"Admin/AdminPage.aspx");
            targetLocations.Add(@"Admin/AdminNews.aspx");
            targetLocations.Add(@"Admin/AdminImages.aspx");
            targetLocations.Add(@"Admin/AdminVideo.aspx");
            targetLocations.Add(@"Admin/AdminAudio.aspx");

            for (int i = 0; i < destList.Items.Count - 1; i++)
            {
                //Add the key, value pair for the current destination to the dictionary
                dictionaryDestinations.Add(destList.Items[i].Text, targetLocations[i]);
            }

            //Default to main admin page (gets the value associated with the dictionary key and sets it as the destination)
            Destination = dictionaryDestinations["Admin Dashboard"];
        }

        public void OnAuthenticated(object o, EventArgs e)
        {
            //TODO: Add SQL Server DB User Authentication Code

            if (Destination != null || Destination != "No Destination")
            {
                //Redirect to the selected destination
                HttpContext.Current.Response.Redirect(Destination);
            }

            //Trigger the Authentication Event (Note: Ensure that this fires only if the user is valid)
            UserAuthenticated(o, e);
        }

        protected override void CreateChildControls()
        {
            lblUserName = new Label();
            lblUserName.Text = "Username: ";

            this.Controls.Add(lblUserName);

            textBoxUserName = new TextBox();

            this.Controls.Add(textBoxUserName);

            Controls.Add(new LiteralControl("<br/>"));

            lblPassword = new Label();
            lblPassword.Text = "Password: ";

            this.Controls.Add(lblPassword);

            textBoxPassword = new TextBox();
            textBoxPassword.TextMode = TextBoxMode.Password;

            this.Controls.Add(textBoxPassword);

            Controls.Add(new LiteralControl("<br/>"));
            Controls.Add(new LiteralControl("<br/>"));

            destList = new DropDownList();
            destList.Items.Add(new ListItem("Admin Dashboard"));
            destList.Items.Add(new ListItem("Add News"));
            destList.Items.Add(new ListItem("Add Photos"));
            destList.Items.Add(new ListItem("Add Audio"));
            destList.Items.Add(new ListItem("Add Video"));
            destList.SelectedIndexChanged += new EventHandler(OnIndexChanged);

            this.Controls.Add(destList);

            Controls.Add(new LiteralControl("<br/>"));

            submitButton = new Button();
            submitButton.Text = "Login";
            submitButton.Click += new EventHandler(OnAuthenticated);

            this.Controls.Add(submitButton);

            this.ChildControlsCreated = true;
        }

        private void OnIndexChanged(object sender, EventArgs e)
        {
            string targetLocation = destList.SelectedValue;

            switch (targetLocation)
            {
                case "Admin Dashboard":
                    //Pass in the value of the location associated with this targetLocation
                    Destination = dictionaryDestinations[targetLocation];
                    break;
                case "Add News":
                    //Pass in the value of the location associated with this targetLocation
                    Destination = dictionaryDestinations[targetLocation];
                    break;
                case "Add Photos":
                    //Pass in the value of the location associated with this targetLocation
                    Destination = dictionaryDestinations[targetLocation];
                    break;
                case "Add Video":
                    //Pass in the value of the location associated with this targetLocation
                    Destination = dictionaryDestinations[targetLocation];
                    break;
                case "Add Audio":
                    //Pass in the value of the location associated with this targetLocation
                    Destination = dictionaryDestinations[targetLocation];
                    break;
                default:
                    //Set the default location
                    Destination = dictionaryDestinations["Admin Dashboard"];
                    break;
            }
        }
    }
}
